// pages/home/home.js
import {
  selectNews,
  selectTz
} from '../../api/check'
import {
  getHomeTj
} from '../../api/statistics'
import dayjs from '../../utils/dayjs.min'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    newsList: [],
    navOpacity: 0,
    tznews: {},
    userInfo: wx.getStorageSync('userinfo'),
    tjData: {},
  },
  // 进入通知的列表页
  toNotice() {
    wx.navigateTo({
      url: '/pages/notice/notice',
    })
  },
  // 进入统计详情
  gotostatistics(e) {
    const index = e.target.dataset.index;
    wx.setStorageSync('statisticsType', index)
    wx.switchTab({
      url: "/pages/statistics/index"
    })
  },
  // 进入安全检查 区分企业自查和专家检查
  toaqjc() {
    if (wx.getStorageSync("userinfo").userlx == 1) {
      // 企业自查
      wx.navigateTo({
        url: '/pages/check/enterprise/index'
      })
    } else if (wx.getStorageSync("userinfo").userlx == 2) {
      // 专家检查 进入企业列表
      wx.navigateTo({
        url: '/pages/check/list/index'
      })
    }
  },
  getTjData() {
    getHomeTj().then(res => {
      this.setData({
        tjData: res.data
      })
    })
  },
  toaqzg() {
    if (wx.getStorageSync("userinfo").userlx == 1) {
      // 企业自整改
      wx.navigateTo({
        url: '/pages/error/dev/index'
      })
    } else if (wx.getStorageSync("userinfo").userlx == 2) {
      // 专家填报整改
      wx.navigateTo({
        url: '/pages/rectification/expert/index'
      })
    }
  },
  toInfo() {
    wx.navigateTo({
      url: '/pages/newsDetail/newsDetail?id=' + this.data.tznews.id + '&type=info',
    })
  },
  tobgck() {
    wx.navigateTo({
      url: '/pages/audit/list/index'
    })
  },
  // 跳转更多新闻页面
  toMore() {
    wx.navigateTo({
      url: '/pages/news/newslist/list',
    })
  },
  toNewsDetail(e) {
    wx.navigateTo({
      url: '/pages/news/newsDetail/newsDetail?id=' + e.currentTarget.dataset.item.id + '&type=news',
    })
  },
  scroll(e) {
    this.setData({
      navOpacity: Math.floor(e.detail.scrollTop) / 100
    })
  },
  getList() {
    selectTz({
      limit: 1,
      page: 1
    }).then(res => {
      this.setData({
        tznews: res.data[0]

      })
    })
    selectNews({
      limit: 3,
      page: 1
    }).then(res => {
      this.setData({
        newsList: res.data
      })
    })
  },
  gotoDetail(e) {
    const item = e.mark.item;
    let form = {
      qh: '',
      zwbj: '',
      hy: '',
      startDate: dayjs().format('YYYY-MM-DD'),
      endDate: dayjs().format('YYYY-MM-DD'),
    }
    wx.navigateTo({
      url: '/pages/statistics/components/detail/index?yh=' + item.bm + '&name=' + item.mch + '&params=' + JSON.stringify(form),
      success: function () {}
    })
  },
  gotoDetail2(e) {
    const item = e.mark.item;
    let form = {
      qh: '',
      zwbj: '',
      hy: '',
      startDate: '',
      endDate: '',
    }
    wx.navigateTo({
      url: '/pages/statistics/components/detail/index?yh=' + item.bm + '&name=' + item.mch + '&params=' + JSON.stringify(form),
      success: function () {}
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      userInfo: wx.getStorageSync('userinfo')
    })
    this.getList()
    if (this.data.userInfo.userlx == 0 || this.data.userInfo.userlx == 3) {
      this.getTjData()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})